package com.zhr.tea.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhr.tea.R;
import com.zhr.tea.api.ShouyeService;
import com.zhr.tea.entity.shouye.chayi.Chayi;
import com.zhr.tea.view.adapter.ChayiAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

public class ChayiActivity extends AppCompatActivity implements ChayiAdapter.RecycleListen {
    @BindView(R.id.rlv)
    XRecyclerView rlv;
    private List<Chayi.TeaDoctorListBean> data = new ArrayList<>();
    private ChayiAdapter adapter;
    private int beginId = 0;
    private int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chayi);
        ButterKnife.bind(this);
        initView();
        initData();
        addHead();
    }

    private void addHead() {
        View view = LayoutInflater.from(this).inflate(R.layout.item_chayi_head, null);
        rlv.addHeaderView(view);

        rlv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initData();
            }

            @Override
            public void onLoadMore() {

                beginId +=pow(2, i);
                i++;
                initData();
            }
        });
    }

    public long pow(long x, int n) {
        long s = 10;
        for (int j = 0; j < n; j++) {
            s = s * x;
        }
        return s;
    }

    private void initData() {
        RequestParams params = new RequestParams();
        params.addHeader("Content-Type", "application/json");
        JSONObject obj = new JSONObject();
        obj.put("memberId", "mt20161031215643782");
        obj.put("phoneNum", "18272162916");
        obj.put("cateType", "");
        obj.put("key", "");
        obj.put("beginId", beginId + "");
        obj.put("count", 10);
        params.applicationJson(obj);
        HttpRequest.post(ShouyeService.CHAYI_URL, params, new BaseHttpRequestCallback<Chayi>() {
            @Override
            protected void onSuccess(Chayi chayi) {
                super.onSuccess(chayi);
                data.addAll(chayi.getTeaDoctorList());
                adapter.notifyDataSetChanged();
                rlv.loadMoreComplete();
                rlv.refreshComplete();
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                super.onFailure(errorCode, msg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
            }
        });

    }

    private void initView() {
        adapter = new ChayiAdapter(this, data, this);
        rlv.setLayoutManager(new LinearLayoutManager(this));
        rlv.setAdapter(adapter);
    }

    @Override
    public void listen(View view, int position) {
        Intent intent = new Intent(this, ChayiEnterActivity.class);
        intent.putExtra("cha", data.get(position));
        startActivity(intent);
    }
}
